package com.lm.activiti.boot.mq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import javax.jms.*;
import java.io.IOException;
import java.util.Objects;

/**
 * @author lm_jcf
 * @date 2020/8/11 15:57
 */
public class ActiveMq {


    @Test
    public void producer() throws JMSException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://192.168.120.236:61616");

        Connection connection = activeMQConnectionFactory.createConnection();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建队列
        Queue queue = session.createQueue("queue_1");

        // 创建消息生产者
        MessageProducer producer = session.createProducer(queue);

        // 创建消息体
        TextMessage message = session.createTextMessage("queue_1");

        // 向队列发送消息
        producer.send(message);

        // 关闭资源
        producer.close();
        session.close();
        connection.close();
        System.out.println("success");

    }

    /**
     * 同个队列多个消费者默认平分消费消息
     */
    @Test
    public void consumer() throws JMSException, IOException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://192.168.120.236:61616");

        Connection connection = activeMQConnectionFactory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建队列
        Queue queue = session.createQueue("queue_1");

        // 创建消费者
        MessageConsumer consumer = session.createConsumer(queue);

        // 1、receive 读取方式：同步阻塞方法
        /*TextMessage receive = (TextMessage) consumer.receive(3000);
        if (Objects.nonNull(receive)) {
            System.out.println("消息:" + receive.getText());
        }*/
        // 2、读取方式：监听器方式
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                try {
                    if (Objects.nonNull(message) && message instanceof TextMessage) {
                        TextMessage receive = (TextMessage) message;
                        System.out.println("Listener 消息:" + receive.getText());
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        // 保证控制台不刷新覆盖
        System.in.read();
        // 关闭资源
        consumer.close();
        session.close();
        connection.close();
        System.out.println("consume success");
    }

    public static final String ACTIVEMQ_URL = "tcp://192.168.122.1:61616";

    /**
     * topic 只能先订阅 再发送消息
     */
    @Test
    public void topicProducer() throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         // 创建主题
        Topic topic = session.createTopic("topic_sub");

        // 创建消息生产者
        MessageProducer producer = session.createProducer(topic);

        // 开启持久化
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        // 创建消息体
        TextMessage message = session.createTextMessage("topic_topic_sub");

        // 向队列发送消息
        producer.send(message);

        // 开启事务时需要手动提交事务
        session.commit();

        // 关闭资源
        producer.close();
        session.close();
        connection.close();
        System.out.println("success");
    }

    @Test
    public void topicConsumer() throws JMSException, IOException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://192.168.120.236:61616");

        Connection connection = activeMQConnectionFactory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建主题
        Topic topic = session.createTopic("topic_1");

        // 创建消费者
        MessageConsumer consumer = session.createConsumer(topic);

        // 2、读取方式：监听器方式
        consumer.setMessageListener(message -> {
            try {
                if (Objects.nonNull(message) && message instanceof TextMessage) {
                    TextMessage receive = (TextMessage) message;
                    System.out.println("topic Listener 消息:" + receive.getText());
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        });

        // 保证控制台不刷新覆盖
        System.in.read();
        // 关闭资源
        consumer.close();
        session.close();
        connection.close();
        System.out.println("consume success");
    }


    @Test
    public void topicConsumer_subscriber() throws JMSException, IOException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();

        connection.setClientID("z3");

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建主题
        Topic topic = session.createTopic("topic_sub");

        // 订阅主题
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remark");

        connection.start();

        Message message = topicSubscriber.receive();

        while (null != message) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("收到的持久化topic:" + textMessage.getText());
            message = topicSubscriber.receive(5000L);

        }

        // 保证控制台不刷新覆盖
        System.in.read();
        // 关闭资源
        session.close();
        connection.close();
        System.out.println("consume success");
    }

}